package cz.czu.pef.DataModelling.entityObjects;

import java.util.*;

import javax.persistence.*;

import org.hibernate.annotations.Index;

/**
 * This entity represents MachineryType, ie. metal cutting machine, building crane, tractor, excavator, etc.
 * 
 * @author rstribrn
 *
 */
@Entity
@Table(name="MACHINE_TYPE")
@Inheritance(strategy=InheritanceType.JOINED)
public class MachineTypeEntityImpl extends IdPersistenceEntityObjectImpl implements IdPersistenceEntityObject
{
	private static final long serialVersionUID = 1794777862769026502L;

	/*
	 * Entity attributes
	 */
	@Column(name="MACHINE_TYPE_NAME", length=200, nullable=false)
	@Index(name="IDX_MACHINE_TYPES_NAME")
	private String machineTypeName;

	@Column(name="MACHINE_TYPE_DESCRIPTION", length=1024)
	private String machineTypeDescription;

	/*
	 * Relations
	 */
	@OneToMany(fetch=FetchType.EAGER, targetEntity=MachineryEntityImpl.class, mappedBy="machineType", cascade={CascadeType.REMOVE})
	private Set<MachineryEntityImpl> machinery;

	public MachineTypeEntityImpl()
	{
		super();
	}

	// TODO: Connect with TrainingTypeEntityImpl/RoleTypeEntityImpl???
	public MachineTypeEntityImpl(String machineTypeName, String machineTypeDescription)
	{
		super();
		this.machineTypeName = machineTypeName;
		this.machineTypeDescription = machineTypeDescription;
	}

	public String getMachineTypeName()
	{
		return machineTypeName;
	}
	
	public void setMachineTypeName(String machineTypeName)
	{
		this.machineTypeName = machineTypeName;
	}
	
	public String getMachineTypeDescription()
	{
		return machineTypeDescription;
	}
	
	public void setMachineTypeDescription(String machineTypeDescription)
	{
		this.machineTypeDescription = machineTypeDescription;
	}

	public Set<MachineryEntityImpl> getMachinery()
	{
		return machinery;
	}

	public List<MachineryEntityImpl> getMachineryAsList()
	{
		return new ArrayList<MachineryEntityImpl>(getMachinery());
	}

    
  public void setMachinery(Set<MachineryEntityImpl> machinery)
  {
    this.machinery = machinery;
  }

    @Override
	public String toString()
	{
		StringBuilder builder = new StringBuilder();
		builder.append("MachineTypeEntityImpl [machineTypeName=");
		builder.append(machineTypeName);
		builder.append(", machineTypeDescription=");
		builder.append(machineTypeDescription);
		builder.append(", toString()=");
		builder.append(super.toString());
		builder.append("]");
		return builder.toString();
	}

	/* (non-Javadoc)
	 * @see cz.czu.pef.DataModelling.domainObjects.MachineType#canBeRemoved()
	 */
	@Override
	public boolean canBeRemoved()
	{
		return (getMachinery().size() == 0);
	}
}
